package com.yun.zf.system.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yun.zf.common.admin.domain.SysAgent;
import com.yun.zf.common.admin.dto.community.AgentInfoDto;
import com.yun.zf.common.admin.model.sys.AgentSearchMo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface SysAgentMapper extends BaseMapper<SysAgent> {

    @Select("<script>" +
            " SELECT st.id ,st.`name`,st.card_no,st.iphone,st.dept_id ,st.`status`,st.email,st.post_id,st.user_id ,st.dept_name ,st.province_name,st.city_name ,st.create_time  , st.del_flag from sys_agent st " +
            "WHERE 1=1 " +
            " <if test=\"agent.startTime != null and agent.endTime != '' \">and  st.create_time BETWEEN #{agent.startTime} AND #{agent.endTime} </if>  " +
            " <if test=\"agent.deptId != null and agent.deptId != 0 \">and  st.dept_id = #{agent.deptId} </if>  " +
            " <if test=\"agent.iphone != null and agent.iphone != '' \">and  st.iphone = #{agent.iphone} </if> " +
            " <if test=\"agent.status != null  \">and  st.status = #{agent.status} </if>  " +
            " <if test=\"agent.name != null and agent.name != '' \">and  st.name = #{agent.name} </if>  " +
            " <if test=\"agent.cityCode != null and agent.cityCode != 0 \">and  st.city_code = #{agent.cityCode} </if>  " +
            " <if test=\"agent.provinceCode != null and agent.provinceCode != 0 \">and  st.province_code = #{agent.provinceCode} </if>  " +
            "</script>")
    public List<SysAgent> getAgent(@Param("agent") AgentSearchMo searchMo,Page<SysAgent> page);

    @Select("<script>" +
            " UPDATE sys_agent set `status`=1 , del_flag =2 WHERE id = #{id}" +
            "</script>")
    public  void  motifyAgentById(Long id);

    @Select("<script>" +
            "  SELECT agent_no, dept_id,name , dept_name  from sys_agent ag WHERE  city_code=#{cityCode}  and ag.agent_no=#{agentNo} " +
            "</script>")
    public AgentInfoDto getAgentInfo(int agentNo,Long cityCode);

    @Select("<script>" +
            " SELECT  `name` ,iphone , agent_no,avatar, dept_name,remarks,weixin   from sys_agent a WHERE a.dept_id in ( " +
            "SELECT dept3_id from zf_dept_community dc WHERE dc.community_id =#{communityId}) " +
            "</script>")
    public List<AgentInfoDto> getAgentByCommunityId(Long communityId);

    @Select("<script>" +
            "SELECT MAX(agent_no) FROM sys_agent WHERE city_code =#{cityCode}" +
            "</script>")
    String getMaxAgentNo(Long cityCode);


    @Select("<script>" +
            " UPDATE sys_agent set `status`=#{status}  WHERE id = #{id}" +
            "</script>")
    public  void  changeStatus(int id,int status);
}
